﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AMS.DD35.Common;
using AMS.DD35.WcfConnector;
using AMS.DD35.WebMVC3.State;
using AMS.DD35.Common.Data;
using AMS.DD35.WebMVC3.Models;

namespace AMS.DD35.WebMVC3.Controllers
{
    public class CharacterController : Controller
    {
        ICharacterService _service = new CharacterProxy();
        IStateManager _state = new StateManager();
        IStaticLookupService _lookup = new StaticLookupProxy();
        //
        // GET: /Character/

        public ActionResult Index()
        {
            IEnumerable<CharacterData> characters = _service.GetCharactersData(_state.SessionVariable);
            return View(characters);
        }

        //
        // GET: /Character/Details/5

        public ActionResult Details(long id)
        {
            CharacterData character = _service.GetCharacterData(id, _state.SessionVariable);
            return View(character);
        }

        //
        // GET: /Character/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Character/Create

        [HttpPost]
        public ActionResult Create(CharacterData pageData)
        {
            try
            {
                pageData.CharacterID = -1;
                pageData.UserName = _state.UserName;
                _service.SaveCharacter(pageData, _state.SessionVariable);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Character/Edit/5
 
        public ActionResult Edit(long id)
        {
            CharacterData character = _service.GetCharacterData(id, _state.SessionVariable);
            return View(character);
        }

        //
        // POST: /Character/Edit/5

        [HttpPost]
        public ActionResult Edit(CharacterData pageData)
        //public ActionResult Edit(long id, CharacterModel pageData)
        {
            try
            {
                _service.SaveCharacter(pageData, _state.SessionVariable);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Character/Delete/5
 
        public ActionResult Delete(long id)
        {
            CharacterData character = _service.GetCharacterData(id, _state.SessionVariable);
            return View(character);
        }

        //
        // POST: /Character/Delete/5

        [HttpPost]
        public ActionResult Delete(CharacterData pageData)
        //public ActionResult Delete(long id, CharacterModel pageData)
        {
            try
            {
                //_service.DeleteCharacter(pageData);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public IEnumerable<SelectListItem> GetRaces()
        {
            return _lookup.GetRaces().ToSelectList();
        }

        public IEnumerable<SelectListItem> GetRaces(int selectedValue)
        {
            return _lookup.GetRaces().ToSelectList(selectedValue);
        }

        public IEnumerable<SelectListItem> GetDeities()
        {
            return _lookup.GetDeities().ToSelectList();
        }

        public IEnumerable<SelectListItem> GetDeities(int selectedValue)
        {
            return _lookup.GetDeities().ToSelectList();
        }

        public IEnumerable<SelectListItem> GetClasses()
        {
            return _lookup.GetClasses().ToSelectList();
        }

        public IEnumerable<SelectListItem> GetClasses(string selectedValue)
        {
            return _lookup.GetClasses().ToSelectList(selectedValue);
        }

        public IEnumerable<SelectListItem> GetFeats()
        {
            // better as IEnumerable<string>, but where to hide FeatID?
            return _lookup.GetFeats().ToSelectList();
        }

        public SelectList GetSkills()
        {
            // better as IEnumerable<string>, but where to hide SkillID?
            return _lookup.GetSkills().ToSelectList();
        }

        public SelectList GetSexes()
        {
            SelectListItem male = new SelectListItem();
            male.Text = "Male";
            male.Value = male.Text;
            SelectListItem female = new SelectListItem();
            female.Text = "Female";
            female.Value = female.Text;
            return new SelectList(new SelectListItem[]{ male, female }, "Value", "Text");
        }

        //public SelectList GetSexes(string selectedValue)
        //{
        //    SelectListItem male = new SelectListItem();
        //    male.Text = "Male";
        //    male.Value = male.Text;
        //    male.Selected = male.Text == selectedValue;
        //    SelectListItem female = new SelectListItem();
        //    female.Text = "Female";
        //    female.Value = female.Text;
        //    female.Selected = female.Text == selectedValue;
        //    return new SelectList(new SelectListItem[] { male, female }, "Value", "Text", selectedValue);
        //}

        public SelectList GetEthicalAlignments()
        {
            return EnumHelper.ToSelectList<EthicalAlignment>();
        }

        //public IEnumerable<SelectListItem> GetEthicalAlignments(EthicalAlignment selectedValue)
        //{
        //    return EnumHelper.ToSelectList<EthicalAlignment>(selectedValue);
        //}

        public SelectList GetMoralAlignments()
        {
            return EnumHelper.ToSelectList<MoralAlignment>();
        }

        //public IEnumerable<SelectListItem> GetMoralAlignments(MoralAlignment selectedValue)
        //{
        //    return EnumHelper.ToSelectList<MoralAlignment>(selectedValue);
        //}

        public IEnumerable<SelectListItem> GetHeightUnits()
        {
            SelectListItem cms = new SelectListItem();
            cms.Text = "cms";
            cms.Value = cms.Text;
            SelectListItem ins = new SelectListItem();
            ins.Text = "ins";
            ins.Value = ins.Text;
            return new SelectList(new SelectListItem[] { ins, cms }, "Value", "Text");
        }

        //public IEnumerable<SelectListItem> GetHeightUnits(string selectedValue)
        //{
        //    SelectListItem cms = new SelectListItem();
        //    cms.Text = "cms";
        //    cms.Value = cms.Text;
        //    cms.Selected = cms.Text == selectedValue;
        //    SelectListItem ins = new SelectListItem();
        //    ins.Text = "ins";
        //    ins.Value = ins.Text;
        //    ins.Selected = ins.Text == selectedValue;
        //    return new SelectList(new SelectListItem[] { ins, cms }, "Value", "Text");
        //}

        public IEnumerable<SelectListItem> GetWeightUnits()
        {
            SelectListItem kgs = new SelectListItem();
            kgs.Text = "kgs";
            kgs.Value = kgs.Text;
            SelectListItem lbs = new SelectListItem();
            lbs.Text = "lbs";
            lbs.Value = lbs.Text;
            return new SelectList(new SelectListItem[] { lbs, kgs }, "Value", "Text");
        }

        //public IEnumerable<SelectListItem> GetWeightUnits(string selectedValue)
        //{
        //    SelectListItem kgs = new SelectListItem();
        //    kgs.Text = "kgs";
        //    kgs.Value = kgs.Text;
        //    kgs.Selected = kgs.Text == selectedValue;
        //    SelectListItem lbs = new SelectListItem();
        //    lbs.Text = "lbs";
        //    lbs.Value = lbs.Text;
        //    lbs.Selected = lbs.Text == selectedValue;
        //    return new SelectList(new SelectListItem[] { lbs, kgs }, "Value", "Text");
        //}
    }
}
